package com.ionehe.group.common.zzd.module;

import com.alibaba.fastjson.JSON;
import com.alibaba.xxpt.gateway.shared.api.response.OapiCalendarCreateCalendarResponse;
import com.alibaba.xxpt.gateway.shared.api.response.OapiChatGroupV2CreateResponse;
import com.alibaba.xxpt.gateway.shared.api.response.OapiChatSendMsgResponse;
import com.alibaba.xxpt.gateway.shared.api.response.OapiTcV2OpenapiTaskCreateJsonResponse;
import com.google.common.base.Throwables;
import com.ionehe.group.common.io.Response;
import com.ionehe.group.common.zzd.dto.CreateGroupDto;
import com.ionehe.group.common.zzd.dto.CreateTaskDto;
import com.ionehe.group.common.zzd.dto.SendChatMessageDto;
import com.ionehe.group.common.zzd.dto.ZzdContent;
import com.ionehe.group.common.zzd.dto.ZzdResponse;
import com.ionehe.group.common.zzd.resp.AccessToken;
import com.ionehe.group.common.zzd.resp.ZzdTicket;
import com.ionehe.group.common.zzd.resp.ZzdUserAccount;
import com.ionehe.group.common.zzd.session.ZzdSession;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping({"/api/zzd"})
@Api(tags = "浙政钉", value = "浙政钉相关接口服务")
public class ZzdController {
    private static final Logger log = LoggerFactory.getLogger(ZzdController.class);
    @Resource
    private ZzdSession zzdSession;

    @PostMapping({"/create_task"})
    @ApiOperation("创建日程")
    public Response createTask(@RequestBody CreateTaskDto dto) {
        if (dto == null) {
            return Response.no("参数错误");
        } else {
            OapiTcV2OpenapiTaskCreateJsonResponse zzdResponse = this.zzdSession.createTask(dto);
            if (zzdResponse != null && zzdResponse.getContent() != null && zzdResponse.getContent().getData() != null) {
                return Response.yes(zzdResponse.getContent().getData());
            } else {
                log.error("Create task failed, response:{}", JSON.toJSONString(zzdResponse));
                return Response.no("创建待办失败");
            }
        }
    }

    @PostMapping({"/send_chat_msg"})
    @ApiOperation("发送聊天消息")
    public Response sendChatMessage(@RequestBody SendChatMessageDto dto) {
        OapiChatSendMsgResponse zzdResponse = this.zzdSession.sendChatMessage(dto);
        if (zzdResponse != null && zzdResponse.getContent() != null && zzdResponse.getContent().getData() != null) {
            return Response.yes(zzdResponse.getContent().getData());
        } else {
            log.error("Send chat message failed, response:{}", JSON.toJSONString(zzdResponse));
            return Response.no("发送聊天消息失败");
        }
    }

    @PostMapping({"/create_chat_group"})
    @ApiOperation("建群")
    public Response createChatGroup(@RequestBody CreateGroupDto dto) {
        OapiChatGroupV2CreateResponse zzdResponse = this.zzdSession.createChatGroup(dto);
        if (zzdResponse != null && zzdResponse.getContent() != null && zzdResponse.getContent().getData() != null) {
            return Response.yes(zzdResponse.getContent().getData());
        } else {
            log.error("Create chat group, response:{}", JSON.toJSONString(zzdResponse));
            return Response.no("建群失败");
        }
    }

    @PostMapping({"/create_calendar"})
    @ApiOperation("创建日程")
    public Response createCalendar() {
        OapiCalendarCreateCalendarResponse zzdResponse = this.zzdSession.createCalendar();
        if (zzdResponse != null && zzdResponse.getContent() != null && zzdResponse.getContent().getData() != null) {
            return Response.yes(zzdResponse.getContent().getData());
        } else {
            log.error("Create calender, response:{}", JSON.toJSONString(zzdResponse));
            return Response.no("创建日程失败");
        }
    }

    @PostMapping({"/get_access_token"})
    public Response getAccessToken() {
        try {
            ZzdResponse<ZzdContent<AccessToken>> zzdResponse = this.zzdSession.getAccessToken();

            if (zzdResponse != null && zzdResponse.getContent() != null && ((ZzdContent) zzdResponse.getContent()).getData() != null) {
                return Response.yes(((ZzdContent) zzdResponse.getContent()).getData());
            }

            log.error("Get access token from zzd failed, response:{}", JSON.toJSONString(zzdResponse));

            return Response.no("获取AccessToken失败");
        } catch (Exception e) {
            log.error("Get access token error! cause:{}", Throwables.getStackTraceAsString(e));
            return Response.no("获取用户token异常");
        }
    }

    @PostMapping({"/get_jsapi_ticket"})
    public Response getJsApiTicket() {
        try {
            ZzdResponse<ZzdContent<ZzdTicket>> zzdResponse = this.zzdSession.getJsApiTicket();

            if (zzdResponse != null && zzdResponse.getContent() != null && ((ZzdContent) zzdResponse.getContent()).getData() != null) {
                return Response.yes(((ZzdContent) zzdResponse.getContent()).getData());
            }

            log.error("Get access token from zzd failed, response:{}", JSON.toJSONString(zzdResponse));

            return Response.no("获取AccessToken失败");
        } catch (Exception e) {
            log.error("Get access token error! cause:{}", Throwables.getStackTraceAsString(e));
            return Response.no("获取用户AccessToken异常");
        }
    }

    @PostMapping({"/get_app_user_info/{authCode}"})
    public Response<ZzdUserAccount> getDingtalkAppUser(@PathVariable String authCode) {
        try {
            ZzdResponse<ZzdContent<ZzdUserAccount>> zzdResponse = this.zzdSession.getDingtalkAppUser(authCode);

            if (zzdResponse != null && zzdResponse.getContent() != null && ((ZzdContent) zzdResponse.getContent()).getData() != null) {
                ZzdUserAccount userAccount = (ZzdUserAccount) ((ZzdContent) zzdResponse.getContent()).getData();
                return Response.yes(userAccount);
            }

            log.error("Get zzd user info failed, response:{}", JSON.toJSONString(zzdResponse));

            return Response.no("获取用户信息失败");
        } catch (Exception e) {
            log.error("Get zzd user info error! cause:{}", Throwables.getStackTraceAsString(e));
            return Response.no("获取用户信息异常");
        }
    }


    @PostMapping({"/healthtest"})
    @ApiOperation("健康测试")
    public Response<String> healthTest() {
        return Response.yes("succeed");
    }

    @GetMapping({"/healthtest"})
    @ApiOperation("健康测试")
    public Response<String> healthTestGet() {
        return Response.yes("succeed");
    }
}
